package sparrowGui
{
	import flash.display.DisplayObject;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import sparrowGui.event.AlertEvent;
	import sparrowGui.item.Item;
	import sparrowGui.item.LabelItem;
	import sparrowGui.skin.AlertSkin;
	import sparrowGui.utils.PubFun;

	/**
	 * 基础提示窗，带确定｜取消
	 * @author pelephone
	 */	
	public class BaseAlert extends Component
	{
		public static const BTN_PREFIX:String = "btn_act";		//skin里按钮的前缀名
		
		public static const DEFAULT_BTN_STRING:String = "确定";	//默认按钮文字
		
//		public var btnCla:Class = null;				//按钮类,通过此类new出按钮
//		public var btnArr:Array = null;				//按钮数组对应btnStr
		private var btnDis:int = 10;				//按钮之间的间距
		private var btmDis:int = 10;				//底边距
		
		private var _autoClose:Boolean = true;		//是否自动关闭。
		private var _autoHidden:Boolean = true;		//是否自动隐藏(不销毁)。
		
		/**
		 * 构造提示窗
		 * @param s			此提示窗提示文字
		 * @param btnStr	通过字符和"|"生成按钮,不想改变文字可以直接设""
		 * @param vars		样式，详细见AlertVars类
		 */		
		public function BaseAlert(data:Object=null,btnStr:String="确定|取消",argSkin:Sprite=null,btnClass:Class=null)
		{
			_skin = argSkin || new AlertSkin();
			super();
			
			addChild(skin);
			if(btnStr)
				createBtn(btnStr,btnClass);
			if(data) update(data);
			
			skin.addEventListener(MouseEvent.CLICK,onPanClick);
//			this.addChild(contents);
//			contents.selectable = contents.mouseEnabled = false;
//			contents.width = this.width - vars.paddingLeft - vars.paddingRight;
//			contents.x = vars.paddingLeft;
//			contents.y = vars.paddingTop;
//			contents.height = this.height  - vars.paddingTop - vars.paddingBottom - btnObj.height;
		}
		
		public function createBtn(btnStr:String="确定|取消",btnCla:Class=null):void
		{
			if(!btnStr || !btnStr.length) return;
//			btnArr = btnArr || new Array();
			btnCla = (btnCla==null)?LabelItem:btnCla;
			
			// 根据btnStr生成按钮并生成至面板下部
			var tbtnArr:Array = btnStr.split("|"),btnObj:Object;
			for(var i:int=0;i<tbtnArr.length;i++){
				//如果皮肤里有btn_act0,1,2...属性，则直接复制，反之生成新的
				if(skin && skin.getChildByName(BTN_PREFIX+i)){
					btnObj =  skin.getChildByName(BTN_PREFIX+i);
				}else{
					btnObj = new btnCla();
					PubFun.setSimpleButtonText((btnObj as DisplayObject),String(tbtnArr[i]));
					btnObj.name = BTN_PREFIX+i;
//					(btnObj as Sprite).mouseChildren = false;
					skin.addChild(btnObj as DisplayObject);
					
					// 新生成的放至下部
					btnObj.y = height - btnObj.height - btmDis;
					var lx:int = btnObj.width*tbtnArr.length/2 + btnDis*(tbtnArr.length-1)/2;
					btnObj.x = width/2 - lx + (btnObj.width+btnDis)*i;
				}

				if(!(btnObj is DisplayObject)) break;
				if(btnObj is Item){
					(btnObj as Item).update(tbtnArr[i]);
				}
				else if(btnObj is SimpleButton){
					PubFun.setSimpleButtonText((btnObj as SimpleButton),tbtnArr[i]);
				}
			}
		}
		
		/**
		 * 面板点击
		 * @param e
		 */
		private function onPanClick(e:MouseEvent):void
		{
			var btnName:String = e.target.name;
			// 如果前缀匹配则证明是按钮
			if(btnName.indexOf(BTN_PREFIX)==0){
				var btnIndex:int = int(PubFun.delStr(btnName,BTN_PREFIX));
				if(stage) stage.focus = stage;
				var inpTxt:String = (txt_input)?txt_input.text:null;
				sendClose(btnIndex,inpTxt);
				if(_autoClose) dispose();
				if(!_autoClose && _autoHidden) close();
			}
		}
		
		protected function sendClose(btnIndex:int,inpTxt:String):void
		{
			dispatchEvent(new AlertEvent(AlertEvent.ALERT_CLOSE,btnIndex,inpTxt));
		}
		/**
		 * 更新数据
		 */
		public function update(data:Object):void
		{
			if(txt_alert) txt_alert.htmlText = String(data);
		}
		
		/**
		 * 跟dispose不同，close只是关掉，并不断开引用
		 */
		public function close():void
		{
			PubFun.clearDisp(this);
			PubFun.SafeRemoveChild(this);
		}
		
		/**
		 * 断开引用并回收
		 */
		override public function dispose():void
		{
			PubFun.clearDisp(this);
			super.dispose();
			PubFun.SafeRemoveChild(this);
		}
		
		public function set autoClose(value:Boolean):void
		{
			_autoClose = value;
		}
		public function get autoClose():Boolean
		{
			return _autoClose;
		}
		public function get autoHidden():Boolean
		{
			return _autoHidden;
		}
		
		public function set autoHidden(value:Boolean):void
		{
			_autoHidden = value;
		}
		// 
		public function get alertSkin():Sprite 
		{
			return skin;
		}
		
		// 提示窗里的文本
		public function get txt_alert():TextField{ return skin["txt_alert"] as TextField };
		// 提示窗里输入文本
		public function get txt_input():TextField{ return skin["txt_input"] as TextField };
	}
}